package pierwszy;

import java.util.Map;
import java.util.TreeMap;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Test
public class CommissionTranslation {
	static WebDriver driver;
    static WebDriverWait wait;
    
    private String PortalLink = "http://www.bermar2.home.pl/ks_portal/login.php";
    private String login = "p.jaworek";
    private String password = "jawso98*";
    
	private String site = "ks_portal";
	private String iconPlusOption = "Zlecenie zwykłe ";
	private String iconPlusOptionCheck = "Zlecenia";   
	private String commissionCategory = "Translacja";
	private String AmountOfCompaniesAfter = "";
	private String AmountOfCompaniesBefore = "";
	
	private KsPortalPage KSP;
	
	private Map<String,String> commissionTranslationData = new TreeMap<String,String>();
   
    @BeforeClass
    void setUp() {
    	System.out.println("Przed klasą");
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver,10);
        driver.manage().window().maximize();
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
		commissionTranslationData.put("Title", "Testowy tytuł zlecenia zwykłego.");
		commissionTranslationData.put("Description", "Testowy opis zlecenia zwykłego.");
		commissionTranslationData.put("Date", "2014-07-14");
		commissionTranslationData.put("Hour", "14");
		commissionTranslationData.put("Minute", "15");
		commissionTranslationData.put("Customer", "Bermar Firma Test");
		commissionTranslationData.put("Accountant", "  Clement (clement)");
		
    	driver.get(PortalLink);
    }
    
    @AfterClass
    static void tearDown() {
    	System.out.println("Za klasą");
    	//driver.quit();
        //driver.close();
    }
    
    @BeforeMethod
    void setUpTest(){
    	System.out.println("Przed metodą testową");
    }
    
    @AfterMethod
    void tearDownTest(){
    	System.out.println("Za metodą testowa");
    }
	
	
	@Test
	public void addCommissionTranslation(){
		
		LogInPage Login = new LogInPage(driver, wait); 
		MainPage MP = Login.logIn(login,password);
				
		//Choose and Check KsPortal site	
		KSP = MP.chooseSite(site);
		
		//Check if KsPortal was chosen.
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.passSelectedSite(site)));
		assert driver.findElement(UiMapping.passSelectedSite(site)).getText().trim().equals(site) : "Site " + site + " was not chosen.";
		
		//Choose and Check IconPlusOption (Employee)	
		KSP.getOptionFromIconPlus(iconPlusOption);
				
		//Check if Employee option was chosen.
		wait.until(ExpectedConditions.presenceOfElementLocated(UiMapping.IconPlusCheck));
		assert driver.findElement(UiMapping.IconPlusCheck).getText().equals(iconPlusOptionCheck) : "Option " + iconPlusOption + " was not chosen.";

		//Fill in commission translation form
		KSP.fillCommissionTranslationForm(commissionCategory, commissionTranslationData);
		
		//Submit commission translation form
		KSP.submitCommissionTranslationForm();
		AmountOfCompaniesBefore = KSP.getAmountOfElements();
	}
	
	@Test (dependsOnMethods="addCommissionTranslation")
	public void findCommissionById() {
		assert KSP.findCommissionTranslationLastId(commissionTranslationData): "Commission was not found!";
	}
	
	@Test (dependsOnMethods="findCommissionById")
	public void checkCommissionData() {
		assert KSP.checkCommisionTranslationData(commissionCategory, commissionTranslationData): "Commission's saved and stored data is different.";
	}
	
	@Test (dependsOnMethods="checkCommissionData")
	public void removeCommissionData() {
		
		KSP.removeCommissionTranslation();
		
		assert KSP.findCommissionTranslationById(KSP.getTranslationId()): "Commission was found!";
		AmountOfCompaniesAfter = KSP.getAmountOfElements();

		assert (Integer.parseInt(AmountOfCompaniesBefore)-Integer.parseInt(AmountOfCompaniesAfter)==1): "Commission was not deleted!";
	}

}
